/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Converters;

import DAO.NoticiaDAO;
import bean.Noticia;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

/**
 *
 * @author jorgefspf
 */
@FacesConverter(forClass = Noticia.class)
public class NoticiaConverter implements Converter {

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
         if(value==null || value.isEmpty() ){
            return null;
        }
        return new NoticiaDAO().buscarNoticia((Integer.valueOf(value))) ;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        Noticia noticia = (Noticia) value;
        System.out.println("O id do noticia no converter:" + noticia.getId());
        if(noticia == null || noticia.getId() <= 0){
            return null;
        }
        return String.valueOf(noticia.getId());
    }
    
}
